package com.gsww.vue.utils;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;




/**
 * Json帮助类
 * @author feiy
 */
public class JsonHelper {
	/**
	 * 将一个对象转换为jsonObject
	 * @param obj
	 * @return jsonObject
	 */
	public static Object convertToJsonObject(Object obj) {
		return JSONObject.fromObject(obj);
	}

	/**
	 * 将一个对象转换为JsonArray
	 * @param obj
	 * @return JsonArray
	 */
	public static Object convertToJsonArray(Object obj) {
		return JSONArray.fromObject(obj);
	}

	/**
	 * 默认返回的成功Json
	 * @return JsonObject
	 */
	public static Map<String, Object>  jsonSuccess(Object obj) {
		return convertToJson(true, "", obj);
	}
	
	/**
	 * 默认返回的失败Json
	 * @param errorInfo
	 * @return JsonObject
	 */
	public static Map<String, Object>  jsonError(String errorInfo) {
		return convertToJson(false, errorInfo, null);
	}

	/**
	 * 返回的成功Json,返回相应的参数
	 * @return JsonObject
	 */
	public static Object jsonParamSuccess(String succinfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 成功
		map.put("success", true);
		map.put("msg", succinfo);
		return JSONObject.fromObject(map);
	}

	/**
	 * 返回的成功Json,返回相应的对象
	 * @return JsonObject
	 */
	public static Object jsonObjectSuccess(Object object) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 成功
		map.put("success", true);
		map.put("data", object);
		map.put("msg", "");
		return JSONObject.fromObject(map);
	}

	

	/**
	 * 向response输出结果
	 * @param obj
	 */
	public static void jsonOut(HttpServletResponse response,Object obj) {
		jsonOut(response,obj, false);
	}

	/**
	 * 上传文件向response输出结果
	 * @param obj
	 * @param fileUpload 是否是上传文件
	 */
	public static void jsonOut(HttpServletResponse response,Object obj, boolean fileUpload) {
		try {
			if (fileUpload)
				response.setContentType("text/html; charset=utf-8");
			else
				response.setContentType("text/json; charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print(obj);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 将Page对象转换为Json格式数据
	 * @param page
	 * @return Object
	 */
	@SuppressWarnings("rawtypes")
	public static Object convertListToJson(List list) {
		if (list == null)
			return "";
		Map<String, Object> map = new HashMap<String, Object>();
		// totalCount总行数
		map.put("totalCount", list.size());
		// root数据
		map.put("root", JSONArray.fromObject(list));
		// return JSONObject
		return JSONObject.fromObject(map);
	}

	/**
	 * 默认返回Json
	 * @return Object
	 */
	public static Map<String, Object> convertToJson(boolean success,String errorInfo,Object obj) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 成功
		if (success) {
			map.put("success", true);
			map.put("data", obj);
		}
		// 失败
		else {
			map.put("success", false);
			map.put("data", "");
		}
		
		map.put("msg", errorInfo);
		
		
		 return map;
		//return JSONObject.fromObject(map);
	}

	/**
	 * Ext保存返回json
	 * @param key 主键
	 * @param request
	 * @return JSONObject
	 */
	public static Object jsonSaveSuccess(String key, HttpServletRequest request) {
		if (key == null || request == null || request.getParameter("index") == null)
			return jsonSuccess(null);
		int index = StringHelper.convertToInt(request.getParameter("index"));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", true);
		result.put("key", key);
		result.put("index", index);
		return convertToJsonObject(result);
	}

	/**
	 * 将树对象转换为Json数据
	 * @return
	 */
	public static Object convertTreeToJson(Object obj) {
		return obj;
	}
}
